package com.coolgor.coldot.dao.auth;

import java.util.List;

import com.coolgor.coldot.entity.auth.Role;
import org.apache.ibatis.annotations.*;

@Mapper
public interface RoleDao {

	@Select("""
		<script>
			SELECT
				*
			FROM
				tb_coldot_auth_role
			WHERE
				role_id = #{roleID}
		</script>			
	""")
	@Results(id = "RoleMap", value = {
		@Result(property = "roleID", column = "role_id"),
		@Result(property = "roleName", column = "role_name"),
		@Result(property = "parentID", column = "parent_id"),
		@Result(property = "priority", column = "priority"),
		@Result(property = "remark", column = "remark")
	})
	Role queryRoleById(Long roleID);


	@Select("""
		<script>
			SELECT
				*
			FROM
				tb_coldot_auth_role
			WHERE
				role_id = #{parentID}
			ORDER BY
				priority DESC
		</script>			
	""")
	@ResultMap("RoleMap")
	List<Role> queryRoleTreeByParentID(@Param("parentID") Long parentID);


	@Select("""
		<script>
			SELECT
				*
			FROM
				tb_coldot_auth_role
			<where>		
				parent_id
				IN
				<foreach collection="parentIDs" item="item" index="index" open="(" separator="," close=")">
					${item}
				</foreach>
			</where>
			ORDER BY
				parent_id,priority
		</script>			
	""")
	@ResultMap("RoleMap")
	List<Role> queryRoleListByParentIDs(@Param("parentIDs") List<Long> parentIDs);


	@Select("""
		<script>
			SELECT
				t2.*
			FROM
				tb_coldot_auth_role_user t1
				LEFT JOIN
				tb_coldot_auth_role t2
				ON t1.role_id=t2.role_id
			where
				t1.user_id = #{userID}
			ORDER BY
				t1.priority DESC
		</script>			
	""")
	@ResultMap("RoleMap")
	List<Role> queryRoleListByUserID(@Param("userID") Long userID);


	@Select("""
		<script>
			SELECT
				*
			FROM
				tb_coldot_auth_role
			<where>
				<if test="roleCondition.roleID != 0">
					and role_id = #{roleCondition.roleID}
				</if>
				<if test="roleCondition.roleName != null">
					and role_name like '%${roleCondition.roleName}%'
				</if>
				<if test="roleCondition.parentID != 0">
					and parent_id = #{roleCondition.parentID}
				</if>
				<if test="roleCondition.priority != null">
					and priority = #{roleCondition.priority}
				</if>
				<if test="roleCondition.remark != null">
					and remark like '%${roleCondition.remark}%'
				</if>
			</where>
			LIMIT #{rowIndex},#{pageSize};
		</script>			
	""")
	@ResultMap("RoleMap")
	List<Role> queryRoleList(@Param("roleCondition") Role roleCondition,
										@Param("rowIndex") int rowIndex,
										@Param("pageSize") int pageSize);


	@Select("""
		<script>
			SELECT
				count(1)
			FROM
				tb_coldot_auth_role
			<where>
				<if test="roleCondition.roleID != 0">
					and role_id = #{roleCondition.roleID}
				</if>
				<if test="roleCondition.roleName != null">
					and role_name like '%${roleCondition.roleName}%'
				</if>
				<if test="roleCondition.parentID != 0">
					and parent_id = #{roleCondition.parentID}
				</if>
				<if test="roleCondition.priority != null">
					and priority = #{roleCondition.priority}
				</if>
				<if test="roleCondition.remark != null">
					and remark like '%${roleCondition.remark}%'
				</if>
			</where>
		</script>			
	""")
	int queryRoleCount(@Param("roleCondition") Role roleCondition);


	@Insert("""
		<script>
			INSERT INTO	tb_coldot_auth_role
				(role_name,parent_id,priority,remark)
			VALUES
				(#{roleName},#{parentID},#{priority},#{remark})
		</script>			
	""")
	@Options(useGeneratedKeys = true, keyColumn = "role_id", keyProperty = "roleID")
	int insert(Role role);


	@Update("""
		<script>
			UPDATE
				tb_coldot_auth_role
			<set>
				<if test="roleName != null">role_name=#{roleName},</if>
				<if test="parentID != null">parent_id=#{parentID},</if>
				<if test="priority != null">priority=#{priority},</if>
				<if test="remark != null">remark=#{remark}</if>
			</set>
			WHERE
				role_id = #{roleID}
		</script>			
	""")
	int update(Role role);


	@Delete("""
		<script>
			DELETE FROM
				tb_coldot_auth_role
			WHERE
				role_id = #{roleID}
		</script>			
	""")
	int delete(Role role);


	@Delete("""
		<script>
			DELETE FROM
				tb_coldot_auth_role
			WHERE
				role_id
				IN
				<foreach collection="list" item="roleID" open="(" separator="," close=")">
					#{roleID}
				</foreach>
		</script>			
	""")
	int deleteBatch(List<Long> roleIDList);
	
}